package org.base.admin.config;

import java.util.Arrays;
import java.util.List;

import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.service.SecurityScheme;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * http://localhost:8088/swagger-ui.html</br>
 * @Api： 描述 Controller</br>
 * @ApiIgnore： 忽略该 Controller，指不对当前类做扫描</br>
 * @ApiOperation： 描述 Controller类中的 method接口</br>
 * @ApiParam：
 * 单个参数描述，与 @ApiImplicitParam不同的是，他是写在参数左侧的。如（ @ApiParam(name="username",value="用户名")Stringusername）</br>
 * @ApiModel： 描述 POJO对象</br>
 * @ApiProperty： 描述 POJO对象中的属性值</br>
 * @ApiImplicitParam： 描述单个入参信息</br>
 * @ApiImplicitParams： 描述多个入参信息</br>
 * @ApiResponse： 描述单个出参信息</br>
 * @ApiResponses： 描述多个出参信息</br>
 * @ApiError： 接口错误所返回的信息
 *
 */

@Configuration
@EnableSwagger2
@ConditionalOnProperty(value="swagger.enable",havingValue="true",matchIfMissing=false)
public class SwaggerConfig {

	@Bean
	public Docket createRestApi() {
		
		return new Docket(DocumentationType.SWAGGER_2)
				.apiInfo(apiInfo())
				.securityContexts(securityContexts())
				.securitySchemes(securitySchemes())
				.select()
				.apis(RequestHandlerSelectors.basePackage("org.base.admin.modules"))
				.paths(PathSelectors.any())
				.build();
				
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder()
				.title("BaseAdmin项目接口文档")
				.description("开发人员项目接口的文档")
				.version("1.0")
				.build();
	}
	
	private List<SecurityScheme> securitySchemes() {
		ApiKey token = new ApiKey("Authorization", "Authorization", "header");
		return Arrays.asList(token);
	}
	
	private List<SecurityContext> securityContexts() {
        return Arrays.asList(SecurityContext.builder()
                        .securityReferences(defaultAuth())
                        .forPaths(PathSelectors.any())
                        .build()
                
        );
    }
    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        return Arrays.asList(new SecurityReference("Authorization", authorizationScopes));
    }

}
